package plugins.schoolcard;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.DistributeSessionlessCache;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.cache.AppCache;
import kd.bos.entity.cache.IAppCache;
import kd.bos.form.events.AfterBindDataEvent;
import kd.bos.form.events.BeforeClosedEvent;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.math.BigDecimal;
import java.util.EventObject;

import static kd.bos.servicehelper.user.UserServiceHelper.cacheKey;

/**
 * 单据界面插件
 */
public class CreatCardHistory extends AbstractBillPlugIn implements Plugin {

    DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

        // 使用相同的缓存区域
        DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");

        // 检查标签
        String tagValue = cache.get("schoolcardtag");
        if ("recharge".equals(tagValue)) {

            // 隐藏消费档口字段
            this.getView().setVisible(false, "dcb4_merchant");



            // 从缓存获取值
            String numberValue = cache.get("schoolcardnumber");


            if (numberValue != null) {
                // 根据number值查询基础资料实体
                QFilter filter = new QFilter("number", QFilter.equals, numberValue);

                // 将字段数组转换为逗号分隔的字符串
                String selectFields = "id"; // 或者如果有多个字段："id,name,number"

                DynamicObject[] schoolCards = BusinessDataServiceHelper.load(
                        "dcb4_schoolcard", // 基础资料实体标识
                        selectFields, // 字段列表字符串
                        new QFilter[]{filter}
                );

                if (schoolCards != null && schoolCards.length > 0) {
                    // 获取基础资料的主键值
                    Long schoolCardId = schoolCards[0].getLong("id");

                    // 设置到目标单据字段
                    this.getModel().setValue("dcb4_schoolcard", schoolCardId);
                    this.getView().updateView("dcb4_schoolcard");
                }

                // 清除缓存（可选）
                cache.remove("schoolcardnumber");
                cache.remove("schoolcardtag");
            }
        }
    }

    @Override
    public void beforeClosed(BeforeClosedEvent e) {
        // this.getView().showMessage("单据即将关闭");

        // this.getView().showMessage("单据已关闭，执行后续操作");

        // 1. 获取单据数据包
        DynamicObject dataEntity = this.getModel().getDataEntity(true);

        // 2. 获取 dcb4_amount 字段值
        BigDecimal amountValue = dataEntity.getBigDecimal("dcb4_amount");
        if (amountValue == null) return;

        // 5. 将值存入缓存（默认1小时有效期）
        cache.put("schoolcardamount", amountValue.toString());

        // this.getView().showMessage("充值金额已保存到缓存: " + amountValue);
    }
}